/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.Praise;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @website https://el-admin.vip
* @author wuhui
* @date 2020-09-24
**/
public interface PraiseRepository extends JpaRepository<Praise, Long>, JpaSpecificationExecutor<Praise> {

    /**
     * 查询点赞数根据动态id
     * @param praiseId
     * @return 成功返回点赞数
     */
    @Query(value = "select count(*) from gzmu_ann_praise WHERE praise_id=:praiseId ",nativeQuery = true)
    int findPraise(@Param("praiseId") String praiseId);

    /**
     * 根据message留言板的id查询对应praise表中的点赞人
     * @param messageId
     * @return 返回动态对应的点赞人信息
     */
    @Query(value = "select * from gzmu_ann_praise WHERE praise_id=:messageId ",nativeQuery = true)
    List<Praise> findMessageUser(@Param("messageId") String messageId);

    /**
     * 添加留言板（根据用户id）
     */
    @Transactional
    @Modifying  //设置当前方法自定义执行的sql语句是INSERT语句，而不是select语句
    @Query(value = " INSERT INTO gzmu_ann_praise(praise_id,user_id,praise_name) VALUES(:#{#praise.praiseId},:#{#praise.userId},:#{#praise.praiseName}) ",nativeQuery = true)
    int insertPraise(Praise praise);
}
